package com.example.seakdreamandroid.Lullaby;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.seakdreamandroid.MyFragment;
import com.example.seakdreamandroid.R;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

/*
* 孔维鑫
*/
//摇篮曲，ASMR，戏曲，冥想学习的列表界面
public class LullabyActivity extends AppCompatActivity {

    private List<Fragment> mFragments; //存放视图
    private ViewPager viewPager;
    private TabLayout mTabLayout;
    private List<String> mtitle; //存放底部标题
    private TextView titleType;
    private ImageView returnLullaby;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lullaby);
        titleType = findViewById(R.id.title_type);
        Intent intent = getIntent();
        String xtitle = intent.getStringExtra("typeMusic");
        titleType.setText(xtitle);
        List<Fragment> mF = initView(xtitle);
        putDate(mF,xtitle);
        //左上角返回键
        returnLullaby = findViewById(R.id.return_lullaby);
        returnLullaby.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }


    //向fragment发送页面信息
    private void putDate(List<Fragment> mF,String xtitle) {
        Fragment fragment0 = mF.get(0);
        Fragment fragment1 = mF.get(1);
//        Fragment fragment2 = mF.get(2);
        Fragment fragment3 = mF.get(2);
        Bundle bundle0 = new Bundle();
        Bundle bundle1 = new Bundle();
        Bundle bundle2 = new Bundle();
        Bundle bundle3 = new Bundle();
        bundle0.putString("xxk",xtitle);
        fragment0.setArguments(bundle0);
        bundle1.putString("xxk",xtitle);
        fragment1.setArguments(bundle1);
        bundle2.putString("xxk",xtitle);
        fragment3.setArguments(bundle2);
    }

    private List<Fragment> initView(String xtitle) {



        mTabLayout = findViewById(R.id.tablayout_lullably);
        viewPager = findViewById(R.id.viewpager_lullably);


        mFragments = new ArrayList<>();
        mFragments.add(new SelectedLullabyFragment(1));
        mFragments.add(new SelectedLullabyFragment(2));
        mFragments.add(new RecommendLullabyFragment(3));

        mtitle = new ArrayList<String>();
        switch (xtitle){
            case "摇篮曲":
                mtitle.add("精选音乐");
                mtitle.add("纯音乐");
//        mtitle.add("自由创作");
                mtitle.add("经典儿歌");
                break;
            case "ASMR":
                mtitle.add("精选");
                mtitle.add("沉浸时刻");
//        mtitle.add("自由创作");
                mtitle.add("官方推荐");
                break;

            case "戏曲":
                mtitle.add("京剧");
                mtitle.add("豫剧");
//        mtitle.add("自由创作");
                mtitle.add("其他");
                break;
            case "冥想学习":
                mtitle.add("精选");
                mtitle.add("纯音乐");
//        mtitle.add("自由创作");
                mtitle.add("场景适配");
                break;
        }





        //实例化适配器
        LullabyAdapter lullabyAdapter = new LullabyAdapter(getSupportFragmentManager(),mFragments,mtitle);
        viewPager.setAdapter(lullabyAdapter);

        mTabLayout.setupWithViewPager(viewPager);//给tab设置一个viewpager
        //viewpager的监听
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override //选中
            public void onPageSelected(int position) {
                switch (position){
                    case 0:
                        Toast.makeText(LullabyActivity.this,"精选音乐",Toast.LENGTH_SHORT);
                        break;

                    case 1:
                        Toast.makeText(LullabyActivity.this,"沉浸时刻",Toast.LENGTH_SHORT);
                        break;
                    case 2:
                        Toast.makeText(LullabyActivity.this,"自由创作",Toast.LENGTH_SHORT);
                        break;
//                    case 3:
//                        Toast.makeText(LullabyActivity.this,"官方推荐",Toast.LENGTH_SHORT);
//                        break;
                }
            }

            @Override //state的状态有三种，0为什么都没有做。1表示正在滑动，2滑动完毕
            public void onPageScrollStateChanged(int state) {

            }
        });
        return mFragments;
    }

}